import 'package:shared_preferences/shared_preferences.dart';
import '../utils/common_utils.dart';
import '../../app_files.dart';

class UserInfoConst {
  static const userid = "userid";
  static const openid = "openid";
  static const nickname = "nickname";
  static const headimg = "headimg";
  static const token = "token";
  static const password = 'password';
  static const phone = 'phone';
  static const position = 'position';
  static const currentPlan = 'currentPlan';
  static const currentPatrolPlan = 'currentPatrolPlan';
}

getSharedPreferencesForKey(String key, SharedPreferences prefs) =>
    prefs.getString(key) ?? '';

class UserInfoEntity {
  String token = '';

  String headimg = '';
  String userid = '';
  String nickname = '用户昵称';
  String department;
  String departmentID;
  // String position;
  // String positionID;
  String mobile = '';
  String phone = '';
  String account = '';
  bool isGsUser = false;

//  bool isLeader = false;
  List<PlanItemModel> plans;
  List<PatrolPlanModel> patrolPlans;
  String password = '';
  int currentPlan = 0;
  int currentPatrolPlan = 0;
  // String hallName = '';
  // String hallID = '';
  bool gender;
  List<String> roleNames = [];
  String currentRoleName = '';
  final List<String> appSupportRoles = [
    // '总经理',
    '安检员',
    // '运营部经理',
    // '安检主任',
    '维修员',
    '巡检员',
    // '工程人员',
    // '客服中心',
    // '市场开发部',
  ];

  UserInfoEntity({
    this.mobile = '',
    this.userid = '',
    this.nickname = '用户昵称',
    this.token = '',
    this.department = '',
    // this.position = '',
    this.headimg = '',
    this.password = '',
    // this.hallID = '',
    // this.hallName = '',
    this.plans,
    this.currentPlan = 0,
    this.currentPatrolPlan = 0,
    this.roleNames,
    this.currentRoleName = '',
  });

  bool get isCountLeader => currentRoleName == '总经理';
  bool get isCheckWorker => currentRoleName == '安检员';
  bool get isPatrolWorker => currentRoleName == '巡检员';
  bool get isCheckLeader =>
      currentRoleName == '运营部经理' || currentRoleName == '安检主任';
  bool get isFixWorker => currentRoleName == '维修员';
  bool get isInstallWorker => currentRoleName == '市场开发部';
  bool get isEngineer => currentRoleName == '工程人员' || currentRoleName == '客服中心';
  bool get isCustomer => currentRoleName == '客服中心';
  String get plantID {
    if (plans == null || plans.isEmpty) return null;
    PlanItemModel data =
        currentPlan < plans.length ? plans[currentPlan] : plans[0];
    return data.planValue;
  }

  String get patrolPlantID {
    if (patrolPlans == null || patrolPlans.isEmpty) return null;
    PatrolPlanModel data = currentPatrolPlan < patrolPlans.length
        ? patrolPlans[currentPatrolPlan]
        : patrolPlans[0];
    return data.planValue;
  }

//  bool get isLeader => false;

  UserInfoEntity.fromJson(Map<String, dynamic> json) {
    userid = CommonUtils.checkNullToString(json['UserId']);
    account = CommonUtils.checkNullToString(json['UserName']);
    // leaderType = json['LeaderType'] ?? 0;
//    leaderType = 3;
    plans = json['Plans'];
//    headimg = CommonUtils.checkNullToString(json['headimg']);
    print('---- user token: ${CommonUtils.checkNullToString(json['Token'])}');
    token = CommonUtils.checkNullToString(json['Token']);
//    mobile = CommonUtils.checkNullToString(json['mobile']);
//    department = CommonUtils.checkNullToString(json['department']);
//    position = CommonUtils.checkNullToString(json['position']);
  }

  UserInfoEntity.fromGasJson(Map<String, dynamic> json) {
    userid = CommonUtils.checkNullToString(json['AppUserId']);
    mobile = CommonUtils.checkNullToString(json['AppUserMobile']);
    token = CommonUtils.checkNullToString(json['Token']);
  }

  UserInfoEntity.fromStaffJson(Map<String, dynamic> json) {
    Map userInfo = json['user'];
    userid = CommonUtils.checkNullToString(userInfo['userId']);
    mobile = CommonUtils.checkNullToString(userInfo['loginName']);
    token = CommonUtils.checkNullToString(json['token']);
    print(token);

    List list = userInfo['roles'];
    userid = CommonUtils.checkNullToString(userInfo['userId']);
    account = CommonUtils.checkNullToString(userInfo['loginName']);
    phone = CommonUtils.checkNullToString(userInfo['phonenumber']);
    headimg = CommonUtils.checkNullToString(userInfo['avatar']);
    nickname = CommonUtils.checkNullToString(userInfo['userName']);
    Map dept = userInfo['dept'];
    department = CommonUtils.checkNullToString(dept['deptName']);
    departmentID = CommonUtils.checkNullToString(dept['deptId']);

    List<String> roles = [];
    list.forEach((element) {
      String role = CommonUtils.checkNullToString(element['roleName']);
      if (appSupportRoles.contains(role)) roles.add(role);
    });
    roleNames = roles;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userid'] = this.userid;
    data['nickname'] = this.nickname;
    data['headimg'] = this.headimg;
    data['token'] = this.token;
    data['mobile'] = this.mobile;
    data['department'] = this.department;
    // data['position'] = this.position;
    data['currentPlan'] = this.currentPlan;
    return data;
  }

  Future<void> save() async {
    await SharedPreferences.getInstance()
      ..setString(UserInfoConst.userid, this.userid)
      ..setString(UserInfoConst.nickname, this.nickname)
      ..setString(UserInfoConst.headimg, this.headimg)
      ..setString(UserInfoConst.password, this.password)
      ..setString(UserInfoConst.phone, this.mobile)
      ..setString(UserInfoConst.position + this.userid, this.currentRoleName)
      ..setInt(UserInfoConst.currentPlan + this.userid, this.currentPlan)
      ..setInt(
          UserInfoConst.currentPatrolPlan + this.userid, this.currentPatrolPlan)
      ..setString(UserInfoConst.token, this.token);
  }

  Future<void> logout() async {
    await SharedPreferences.getInstance()
      ..setString(UserInfoConst.userid, '')
      ..setString(UserInfoConst.phone, '')
      ..setString(UserInfoConst.nickname, '')
      ..setString(UserInfoConst.token, '')
      ..setString(UserInfoConst.password, '')
      ..setString(UserInfoConst.headimg, '');
  }

  setInfoFromJson(Map<String, dynamic> json) {
    List list = json['AdminRoleNames'];
    userid = CommonUtils.checkNullToString(json['AdminId']);
    account = CommonUtils.checkNullToString(json['AdminAccount']);
    phone = CommonUtils.checkNullToString(json['AdminMobile']);
    headimg = CommonUtils.checkNullToString(json['AdminPhoto']);
    nickname = CommonUtils.checkNullToString(json['AdminName']);
    // position = CommonUtils.checkNullToString(json['AdminPstname']);
    // positionID = CommonUtils.checkNullToString(json['AdminPstid']);
    department = CommonUtils.checkNullToString(json['AdminDptname']);
    departmentID = CommonUtils.checkNullToString(json['AdminDptid']);
    // hallName = CommonUtils.checkNullToString(json['ServiceHallName']);
    // hallID = CommonUtils.checkNullToString(json['ServiceHallId']);
    List<String> roles = [];
    list.forEach((element) {
      String role = CommonUtils.checkNullToString(element);
      if (appSupportRoles.contains(role)) roles.add(role);
    });
    roleNames = roles;
  }

  setGasInfoFromJson(Map<String, dynamic> json) {
    userid = CommonUtils.checkNullToString(json['AppUserId']);
    mobile = CommonUtils.checkNullToString(json['AppUserMobile']);
    phone = CommonUtils.checkNullToString(json['AppUserMobile']);
    headimg = CommonUtils.checkNullToString(json['AppUserPhoto']);
    nickname = CommonUtils.checkNullToString(json['AppUserNickName']);
    gender = json['AppUserSex'];
  }
}
